const user = require("../sql/user");

const obj = {
    get(colObj, where, success, error) {
        if (typeof where === "function") {
            success = where;
            where = {};
        }
        colObj.find(where, { _id: 0, __v: 0 }, (err, data) => {
            if (err) {
                error(err);
            } else {
                success(data);
            }
        })
    },
    getSort(colObj, sort, where = {}, success, error) {
        if (typeof where === "function") {
            success = where;
            where = {};
        }
        colObj.find(where, { _id: 0, __v: 0 }).sort(sort).exec((err, data) => {
            if (err) {
                error(err);
            } else {
                success(data);
            }
        })
    },
    getPage(colObj, index, num, where, success, error) {
        if (typeof where === "function") {
            success = where;
            where = {};
        }
        colObj.find(where, { _id: 0, __v: 0 }).limit(num).skip(index * num).exec((err, data) => {
            if (err) {
                error(err);
            } else {
                success(data);
            }
        })
    },
    getKind(colObj, filed, success, error) {
        colObj.distinct(filed).exec((err, data) => {
            if (err) {
                error(err);
            } else {
                success(data);
            }
        })
    },
    getCount(colObj, success, error) {
        colObj.count().exec((err, data) => {
            if (err) {
                error(err);
            } else {
                success(data);
            }
        })
    },
    add(colObj, data, success, error) {
        colObj.insertMany(data, (err) => {
            if (err) {
                error(err);
            } else {
                success();
            }
        })
    },
    del(colObj, where, num, success, error) {
        if (typeof num === "function") {
            success = num;
            num = 1;
        }
        let x = num === 1 ? "deleteOne" : "deleteMany";
        colObj[x](where, (err) => {
            if (err) {
                error(err);
            } else {
                success();
            }
        })
    },
    set(colObj, where, data, num, success, error) {
        if (typeof num === "function") {
            success = num;
            num = 1;
        }
        let x = num === 1 ? "updateOne" : "updateMany";
        colObj[x](where, data, (err) => {
            if (err) {
                error(err);
            } else {
                success();
            }
        })
    }
}
// obj.get(user, {}, data => {
//     console.log(data)
// })
// obj.set(user, { password: "1234567789" }, { password: "1234567789" }), (data) => {
//     // console.log(data)
// }

// obj.set(user, { isLogin: 1 }, { password: "123" }, () => {
//     console.log("修改成功")
// })
// obj.set(user, { isLogin: 1 }, { $inc: { password: "123" } }, () => {
//     console.log("修改成功")
// })

// get(user, { userid }, dat => {
//     console.log(dat)
// })

module.exports = obj;
